package org.example.integration.adapter.org;

import org.example.cache.core.CacheScope;
import org.example.cache.stereotype.CachingMethod;
import org.example.cache.stereotype.MultiLayerCaching;
import org.example.order.domain.Org;
import org.example.order.domain.OrgRepository;
import org.example.org.api.OrgService;
import org.example.org.api.SdkOrgDTO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
@MultiLayerCaching(cacheName = "ORG", scope = CacheScope.REQUEST_GLOBAL)
public class OrgAdapter implements OrgRepository {
    private static final Logger logger = LoggerFactory.getLogger(OrgAdapter.class);
    private OrgService orgService;

    public OrgAdapter(@Autowired OrgService orgService) {
        this.orgService = orgService;
    }

    @CachingMethod()
    @Override
    public Org getOrg(String orgId) {
        SdkOrgDTO sdkOrgDTO = orgService.getOrg(orgId);
        if (logger.isInfoEnabled()) {
            logger.info("\uD83D\uDCBB RPC for Org, key: {}", orgId);
        }
        return OrgConverter.INSTANCE.convert(sdkOrgDTO);
    }

    @CachingMethod()
    @Override
    public List<Org> getOrgs(List<String> orgIds) {
        List<SdkOrgDTO> sdkOrgDTOList = orgService.getOrgs(orgIds);
        if (logger.isInfoEnabled()) {
            logger.info("\uD83D\uDCBB RPC for Orgs, keys: {}", String.join(",", orgIds));
        }
        return OrgConverter.INSTANCE.convert(sdkOrgDTOList);
    }
}
